import subprocess
import time

# 统计变量
kill_count = 0
function_call_count = 0

def check_and_kill_process():
    global kill_count, function_call_count

    # 启动进程
    with open('output.txt', 'a') as f:
        # 打印时间
        f.write(time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())))
        subprocess.Popen(['/root/ummanage/build/linux/x86_64/release/ummange-test'], stdout=f, stderr=f)
        f.flush()

    # 睡眠10s
    time.sleep(20)

    # 检查进程是否仍在运行
    if subprocess.call(['pgrep', 'ummange-test']) == 0:
        # 进程仍在运行，kill进程
        subprocess.call(['pkill', 'ummange-test'])
        kill_count += 1  # 增加进程被kill掉的次数

    function_call_count += 1  # 增加函数被调用的次数

# 定义定时任务函数
def print_statistics(f):
    global kill_count, function_call_count


# 循环调用函数
with open('/root/ummanage/count.txt', 'a') as f:
    while True:
        check_and_kill_process()
        # 时间(年-月-日-时-分-秒)，kill次数，函数调用次数
        f.write(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))},{kill_count},{function_call_count}\n")
        f.flush()



